package com.wyf.interceptors;

import com.alibaba.fastjson.JSONObject;
import com.wyf.utils.JWTUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wyf
 * @date 2022年07月17日 18:55
 */
public class JWTInterceptor implements HandlerInterceptor
{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        JWTUtils jwtUtils = new JWTUtils();
        Map<String,Object> map = new HashMap<>();
        try {
            jwtUtils.verifyToken(token);
            return true;
        } catch (Exception e) {
            map.put("msg","token expired fail");
            JSONObject json = new JSONObject(map);
            response.getWriter().println(json);
        }
        return false;
    }
}
